package com.tools.genxsd.util;

import cn.hutool.core.text.CharSequenceUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @author: zhoubin
 * @date: 2022/11/16
 */
public class FilePathUtil {

    private static Properties properties = new Properties();

    /**
     * 基础类型文件路径
     */
    private static String baseTypeFile;

    /**
     * 基础组件文件路径
     */
    private static String baseElementFile;

    /**
     * 需要组装的报文文件路径
     */
    private static String filePath;

    static {

        try {
            properties.load(new InputStreamReader(new FileInputStream("./path.properties"), "UTF-8"));
        } catch (IOException e) {
            System.out.println("配置文件不存在");
            e.printStackTrace();
        }
        baseTypeFile = getFormatPath("baseTypeFile");
        baseElementFile = getFormatPath("baseElementFile");
        filePath = getFormatPath("filePath");
        System.out.println("基础文件路径为: " + baseTypeFile);
        System.out.println("基础组件文件路径为: " + baseElementFile);
        System.out.println("需要组装的报文文件路径为: " + filePath);
    }

    public static String getBaseTypeFile() {
        return baseTypeFile;
    }

    public static String getBaseElementFile() {
        return baseElementFile;
    }

    public static String getFilePath() {
        return filePath;
    }

    private static String getFormatPath(String proKey) {
        String value = properties.getProperty(proKey);

        if (CharSequenceUtil.isBlank(value)) {
            return null;
        }

        if (value.contains("\\")) {
            value = value.replaceAll("\\\\", "/");
        }
        return value;
    }
}
